//
//  ZJTabBarController.m
//  Lottery
//
//  Created by ZJ on 2018/8/14.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJTabBarController.h"
#import "ZJTabBar.h"

@interface ZJTabBarController ()

@end

@implementation ZJTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupGUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupGUI {
    [self setupViewControllers];
    
    [self setupTabbar];
}

- (void)setupTabbar {
    // 创建一个自定义的 tabbar
    ZJTabBar *tabbar = [[ZJTabBar alloc] init];
    // 设置 tabbar 的 frame 为系统的 frame
    tabbar.frame = self.tabBar.bounds;
    
    // 3.使用 block(遵守协议,使用代理方法)
    __weak typeof(self) weakSelf = self;
    tabbar.tabbarButtonBlock = ^(NSInteger index) {
        weakSelf.selectedIndex = index;
    };
    
    for (int i = 0; i < self.viewControllers.count; i++) {
        // 获取图片
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i + 1]];
        UIImage *imagePress = [UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i + 1]];
        
        [tabbar addButtonWithImage:image imageSelected:imagePress];
    }
    
    // 添加到 tabbarController 中
//    [self.view addSubview:tabbar];
    [self.tabBar addSubview:tabbar];
}

- (void)setupViewControllers {
    // 获取五个子控制器
    UIViewController *v1 = [self loadSubViewController:@"Hall"];
    UIViewController *v2 = [self loadSubViewController:@"Arena"];
    UIViewController *v3 = [self loadSubViewController:@"Discovery"];
    UIViewController *v4 = [self loadSubViewController:@"History"];
    UIViewController *v5 = [self loadSubViewController:@"MyLottery"];
    
    // 设置 tabbarController子控制器
    self.viewControllers = @[v1, v2, v3, v4, v5];
}

// 根据sb 名字返回 这个 sb 箭头所指向的控制器
- (UIViewController *)loadSubViewController:(NSString *)sbName {
    // 获取 sb对象
    UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
    
    return sb.instantiateInitialViewController;
}

@end
